package com.example.administrator.tenyuanpackage;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.SparseArray;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;

import com.example.administrator.tenyuanpackage.fragment.GoodValueFragment;
import com.example.administrator.tenyuanpackage.fragment.HomeFragment;
import com.example.administrator.tenyuanpackage.fragment.PersonalFragment;
import com.example.administrator.tenyuanpackage.speacial.SpeacialOfferFragment;
import com.example.administrator.tenyuanpackage.speacial.TenBuyFragment;

public class MainActivity extends AppCompatActivity {
    private SparseArray<Fragment> mFragments = new SparseArray<>();
    private Toast mBackToast = Toast.makeText(App.context, "再次点击返回键退出", Toast.LENGTH_SHORT);
    private static MainActivity instance;
    private FragmentManager mFragmentManager;
    private Fragment mCurrentFragment;
    private Handler mHandler = new Handler();
    private boolean isFinish = false;
    private Toolbar mToolbar;
    private BottomNavigationView mBottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        instance = this;
         initFragment();
        setUpView();
        initView();

    }

    private void initFragment(){
        Fragment homeFragment = new HomeFragment();
        Fragment speacialOfferFragment = new SpeacialOfferFragment();
        Fragment tenBuyFragmant = new TenBuyFragment();
        Fragment goodValueFragment = new GoodValueFragment();
        Fragment personalFragment = new PersonalFragment();
        mFragments.put(0,homeFragment);
        mFragments.put(1,speacialOfferFragment);
        mFragments.put(3,tenBuyFragmant);
        mFragments.put(2,goodValueFragment);
        mFragments.put(4,personalFragment);
    }
    private void initView(){

        mFragmentManager = getSupportFragmentManager();
        mFragmentManager.beginTransaction().add(R.id.frame_content,mFragments.get(0)).commit();
        mCurrentFragment = mFragments.get(0);
    }
    protected void setUpView() {
       // mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);


        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                //mSortMenu.setVisible(false);
                // Class<?> clazz = null;
                switch (item.getItemId()) {
                    case R.id.item_home:
                        switchFragment(mFragments.get(0));
                        break;
                    case R.id.item_specialoffer:
                        // mSortMenu.setVisible(true);
                        switchFragment(mFragments.get(1));
                        break;
                    case R.id.item_tenbuy:
                        //mSortMenu.setVisible(false);
                        switchFragment(mFragments.get(3));
                        break;
                    case R.id.item_goodvalue:
                        switchFragment(mFragments.get(2));
                        break;
                    case R.id.item_personal:
                        switchFragment(mFragments.get(4));
                        break;

                }
                return true;
            }
        });

    }
    private void switchFragment(Fragment toFragment){
        if (toFragment.isAdded()){
            mFragmentManager.beginTransaction().
                    hide(mCurrentFragment).show(toFragment).commit();
        }else {
            mFragmentManager.beginTransaction().
                    hide(mCurrentFragment).add(R.id.frame_content,toFragment).commit();
        }
        mCurrentFragment = toFragment;
    }



    @Override
    public void onBackPressed() {
        if (isFinish) {
            mBackToast.cancel();
            instance = null;
            finish();
        } else {
            mBackToast.show();
        }
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                isFinish = false;
            }
        }, 2000);
        isFinish = true;
    }
    @Override
    protected void onDestroy() {
        instance = null;
        super.onDestroy();
    }
}
